using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using EntityLayer;
using System.Collections.Generic;

public partial class Admin_Links : SFPage
{
    busLinks insBusLinks = new busLinks();
    entLinks insEntLinks = new entLinks();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgLinks_NeedsDataSource()
    {
        DataTable insDataTableLinks = insBusLinks.SelectLinks();
        if (insBusLinks.HasErrors)
        {
            MessageBoxShow(insBusLinks);
        }
        sfgLinks.SetData(insDataTableLinks);
        return insBusLinks.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgLinks_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntLinks.LinkId = Convert.ToInt32(e.CommandArgument);
            insBusLinks.DeleteLinksByLinkId(insEntLinks);
            if (insBusLinks.HasErrors)
            {
                MessageBoxShow(insBusLinks);
                return;
            }
            sfgLinks.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstLinks.LoadDataListData();

    }
    protected bool lstLinks_NeedsDataSource()
    {
        insEntLinks.LinkId = this.Id;
        insBusLinks.SelectLinksByLinkId(insEntLinks);
        if (insBusLinks.HasErrors)
        {
            MessageBoxShow(insBusLinks);
            return true;
        }
        txtLinkAddress.Text = insEntLinks.LinkAddress;
        entLinkContents insEntLinkContents = new entLinkContents();
        insEntLinkContents.LinkId = insEntLinks.LinkId;
        busLinkContents insBusLinkContents = new busLinkContents();
        DataTable insDataTableLinkContents = insBusLinkContents.SelectLinkContentsByLinkId(insEntLinkContents);
        if (insBusLinkContents.HasErrors)
        {
            MessageBoxShow(insBusLinkContents);
            return true;
        }
        lstLinks.SetData(insDataTableLinkContents);
        return false;
    }


    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }
        if (!WebControls.Validation.ValidateWebSite(txtLinkAddress.Text))
        {
            MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { lblLinkAddress.Text }));
            return;
        }
        insEntLinks.LinkAddress = txtLinkAddress.Text;


        foreach (DataListItem dli in lstLinks.Items)
        {
            entLinkContents insEntLinkContents = new entLinkContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntLinkContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntLinkContents.LinkDescription = ((SFTextBox)dli.FindControl("txtLinkDescription")).Text;
                insEntLinkContents.LinkName = ((SFTextBox)dli.FindControl("txtLinkName")).Text;
                if (lstLinks.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntLinkContents.LinkContentId = Convert.ToInt32(lstLinks.DataKeys[dli.ItemIndex]);
                    insEntLinks.LinkContentsUpdate.Add(insEntLinkContents);
                }
                else
                {
                    insEntLinks.LinkContentsInsert.Add(insEntLinkContents);
                }
            }
            else
            {
                if (lstLinks.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntLinkContents.LinkContentId = Convert.ToInt32(lstLinks.DataKeys[dli.ItemIndex]);
                    insEntLinks.LinkContentsDelete.Add(insEntLinkContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusLinks.InsertLinks(insEntLinks);
        if (insBusLinks.HasErrors)
        {
            MessageBoxShow(insBusLinks);
            return;
        }
        base.SaveNew();
        sfgLinks.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntLinks.LinkId = this.Id;
        insBusLinks.UpdateLinksByLinkId(insEntLinks);
        if (insBusLinks.HasErrors)
        {
            MessageBoxShow(insBusLinks);
            return;
        }
        base.SaveExisting();
        sfgLinks.LoadGridData();
    }

    protected void lstLinks_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["LinkContentId"] != DBNull.Value);
                e.Item.FindControl("pnlLinkContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }

    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlLinkContents").Visible = ((SFCheckBox)sender).Checked;
    }
}
